// 大数越界情况下的求余问题，答案需要取模

var cuttingRope = function (n) {
  if (n <= 3) return n - 1;
  let res = 1;
  while (n > 4) {
    res = (res * 3) % (1e9 + 7);
    n -= 3;
  }
  return (res * n) % (1e9 + 7);
};

// Math.pow 的 for 循环替代
var cuttingRope = function (n) {
  if (n <= 3) return n - 1;
  let a = Math.floor(n / 3);
  b = n % 3;
  if (b == 0) {
    let res = 1;
    for (let i = 0; i < a; i++) {
      res = (res * 3) % (1e9 + 7);
    }
    return res;
  }

  if (b == 1) {
    let res = 4;
    for (let i = 0; i < a - 1; i++) {
      res = (res * 3) % (1e9 + 7);
    }
    return res;
  }

  if (b == 2) {
    let res = 2;
    for (let i = 0; i < a; i++) {
      res = (res * 3) % (1e9 + 7);
    }
    return res;
  }
};
